﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Master_Reminder : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0 || Request.QueryString != null)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["mode"]))
                {
                    if (Request.QueryString["mode"].ToString() != string.Empty && Request.QueryString["mode"].ToString() == "saved")
                    {
                        lblMsg.Text = "Reminder Saved Successfully !!";
                    }
                }
            }
            BindVoucherType();
            BindLedger();
        }
       
    }

    protected void BindVoucherType()
    {
        strsql = "select TYPEID,TRANSACTIONNAME from ACC_TRANSETTINGS where ACC_TRANSETTINGS.TYPEID=ACC_TRANSETTINGS.PARENTTYPEID and ACC_TRANSETTINGS.PARENTTYPEID <>5 ";
        DataTable dtVoucherType = DU.getdata(strsql);
        ddlVoucherType.DataSource = dtVoucherType;
        ddlVoucherType.DataTextField = "TRANSACTIONNAME";
        ddlVoucherType.DataValueField = "TYPEID";
        ddlVoucherType.DataBind();
        ddlVoucherType.Items.Insert(0, new ListItem("-Select-", "0"));
    }

    protected void BindLedger()
    {
        strsql = "select LID,LNAME from ACC_LEDMAST";
        DataTable dtLedger = DU.getdata(strsql);

        ddlDebitLedger.DataSource = dtLedger;
        ddlDebitLedger.DataTextField = "LNAME";
        ddlDebitLedger.DataValueField = "LID";
        ddlDebitLedger.DataBind();
        ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));

        ddlCreditLedger.DataSource = dtLedger;
        ddlCreditLedger.DataTextField = "LNAME";
        ddlCreditLedger.DataValueField = "LID";
        ddlCreditLedger.DataBind();
        ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
    }


    protected void BindLedgerDebitCredit()
    {
       
        if (ddlVoucherType.SelectedIndex == 1)
        {
            SqlParameter[] sparam1 = {
                                    new SqlParameter("@TransType","3")// 1 for Debit
                                };

            DataTable DT1 = DU.getdatausingprocedure("USP_ACC_GetLedgerForContraTransactionForDebit", sparam1);
            if (DT1.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT1;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
           
                ddlCreditLedger.DataSource = DT1;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 2)
        {
            SqlParameter[] sparam3 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")// 2 for Debit
                                };
            DataTable DT3 = DU.getdatausingprocedure("USP_ACC_GetLedgerForCreditNoteTransaction", sparam3);
            if (DT3.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT3;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam4 =
                                {
                                    new SqlParameter("@CreditOrDebit","0") // 1 for Credit
                                };
            DataTable DT4 = DU.getdatausingprocedure("USP_ACC_GetLedgerForCreditNoteTransaction", sparam4);
            if (DT4.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT4;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 3)
        {
            SqlParameter[] sparam5 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT5 = DU.getdatausingprocedure("USP_ACC_GetLedgerForDebitNoteTransaction", sparam5);
            if (DT5.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT5;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam6 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT6 = DU.getdatausingprocedure("USP_ACC_GetLedgerForDebitNoteTransaction", sparam6);
            if (DT6.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT6;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 4)
        {
            SqlParameter[] sparam7 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT7 = DU.getdatausingprocedure("USP_ACC_GetLedgerForJournalTransaction", sparam7);
            if (DT7.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT7;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
             SqlParameter[] sparam8 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT8 = DU.getdatausingprocedure("USP_ACC_GetLedgerForJournalTransaction", sparam8);
            if (DT8.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT8;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 5)
        {
            SqlParameter[] sparam9 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT9 = DU.getdatausingprocedure("USP_ACC_GetLedgerForPaymentTransaction", sparam9);
            if (DT9.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT9;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam10 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT10 = DU.getdatausingprocedure("USP_ACC_GetLedgerForPaymentTransaction", sparam10);
            if (DT10.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT10;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 6)
        {
            SqlParameter[] sparam11 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT11 = DU.getdatausingprocedure("USP_ACC_GetLedgerForPurchaseTransaction", sparam11);
            if (DT11.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT11;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam12 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT12 = DU.getdatausingprocedure("USP_ACC_GetLedgerForPurchaseTransaction", sparam12);
            if (DT12.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT12;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 7)
        {
            SqlParameter[] sparam13 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT13 = DU.getdatausingprocedure("USP_ACC_GetLedgerForPurchaseReturnTransaction", sparam13);
            if (DT13.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT13;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam14 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT14 = DU.getdatausingprocedure("USP_ACC_GetLedgerForPurchaseReturnTransaction", sparam14);
            if (DT14.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT14;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 8)
        {
            SqlParameter[] sparam15 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT15 = DU.getdatausingprocedure("USP_ACC_GetLedgerForReceiptTransaction", sparam15);
            if (DT15.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT15;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam16 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT16 = DU.getdatausingprocedure("USP_ACC_GetLedgerForReceiptTransaction", sparam16);
            if (DT16.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT16;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 9)
        {
            SqlParameter[] sparam17 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT17 = DU.getdatausingprocedure("USP_ACC_GetLedgerForSaleTransaction", sparam17);
            if (DT17.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT17;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam18 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT18 = DU.getdatausingprocedure("USP_ACC_GetLedgerForSaleTransaction", sparam18);
            if (DT18.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT18;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else if (ddlVoucherType.SelectedIndex == 10)
        {
            SqlParameter[] sparam19 =
                                {
                                    new SqlParameter("@CreditOrDebit","1")
                                };
            DataTable DT19 = DU.getdatausingprocedure("USP_ACC_GetLedgerForSaleRetrunTransaction", sparam19);
            if (DT19.Rows.Count > 0)
            {
                ddlDebitLedger.DataSource = DT19;
                ddlDebitLedger.DataTextField = "LNAME";
                ddlDebitLedger.DataValueField = "LID";
                ddlDebitLedger.DataBind();
                ddlDebitLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
            SqlParameter[] sparam20 =
                                {
                                    new SqlParameter("@CreditOrDebit","0")
                                };
            DataTable DT20 = DU.getdatausingprocedure("USP_ACC_GetLedgerForSaleRetrunTransaction", sparam20);
            if (DT20.Rows.Count > 0)
            {
                ddlCreditLedger.DataSource = DT20;
                ddlCreditLedger.DataTextField = "LNAME";
                ddlCreditLedger.DataValueField = "LID";
                ddlCreditLedger.DataBind();
                ddlCreditLedger.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
    }

    protected void ddlVoucherType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindLedgerDebitCredit();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string strRemDate = string.Empty;
            string strActiveFrmDate = string.Empty;
            if (txtRemDate.Text != string.Empty)
            {
                string[] temparr = txtRemDate.Text.ToString().Split('/');
                strRemDate = temparr[1] + "/" + temparr[0] + "/" + temparr[2];
            }
            if (txtActiveFrom.Text != string.Empty)
            {
                string[] temparr = txtActiveFrom.Text.ToString().Split('/');
                strActiveFrmDate = temparr[1] + "/" + temparr[0] + "/" + temparr[2];
            }
            if (ddlVoucherType.SelectedValue != "0" && ddlDebitLedger.SelectedValue != "0" && ddlCreditLedger.SelectedValue != "0" && txtRemName.Text != "")
            {
                SqlParameter[] sqlParameter = { 
                                                  new SqlParameter("@ID","0"),
                                                  new SqlParameter("@TYPE",ddlVoucherType.SelectedValue),
                                                  new SqlParameter("@NAME",txtRemName.Text),
                                                  new SqlParameter("@DATE",strRemDate),
                                                  new SqlParameter("@DEBIT_LID",ddlDebitLedger.SelectedValue),
                                                  new SqlParameter("@CREDIT_LID",ddlCreditLedger.SelectedValue),
                                                  new SqlParameter("@ALERT_BEFORE_DAYS",txtAlertDays.Text),
                                                  new SqlParameter("@ISACTIVE",rdbtnlstAlertStatus.SelectedValue),
                                                  new SqlParameter("@ACTIVATE_FROM_DATE",strActiveFrmDate),
                                                  new SqlParameter("@FREQUENCY",ddlFreqRemind.SelectedValue),
                                                  new SqlParameter("@REMARKS",txtRemarks.Text),
                                                  new SqlParameter("@CNAME",Session["username"].ToString()),
                                                  new SqlParameter("@ENAME","")
                                              };
                int Result = DU.executeprocedure("USP_MAS_SaveReminder", sqlParameter);
                if (Result > 0)
                {
                    Response.Redirect("Reminder.aspx?mode=saved");
                }
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/welcome.aspx");
    }
}
